(function(angular) {
	"use strict";
	angular.module("fs.waf").factory("repaymentService", repaymentService);
	repaymentService.$inject = [ "$q", "ajaxServiceFactory", "arithService", "txnService" ];
	function repaymentService($q, ajaxServiceFactory, arithService, txnService) {
		var dataType = "REPAYMENT";

		function getRepaymentLayout(prodSubtype, prodType, action) {
			return txnService.getLayout("repayment", action, false, dataType, prodType, prodSubtype);
		}

		function getWriteOffLayout(prodSubtype, prodType, action) {
			return txnService.getLayout("writeOff", action, false, dataType, prodType, prodSubtype);
		}

		function getRolloverLayout(prodSubtype, prodType, action) {
			return txnService.getLayout("rollover", action, false, "LOAN", prodType, prodSubtype);
		}

		function getLoanViewRepaymentLayout(prodSubtype, prodType) {
			return txnService.getLayout("popup", "repaymentView", true, dataType, prodType, prodSubtype);
		}

		function initData(repayment) {
			if (repayment && repayment.loan && repayment.loan.loanInfo && repayment.loan.loanInfo.prodCode) {
				repayment.loan.loanInfo.prodName = repayment.loan.loanInfo.prodCode.substr(7);
			}
		}

		function finalRepayFlagChanged(repayment) {
			if (repayment.finalRepayFlag == "Y") {
				if (repayment.loan && repayment.loan.loanExt) {
					if (repayment.repayType == "W") {
						repayment.repayAmt = repayment.loan.loanExt.princOutstdAmt;
					} else {
						repayment.repayPrincAmt = repayment.loan.loanExt.princOutstdAmt;
					}
				}
				repayment.closeTxnFlag = repayment.closeTxnFlag || "Y";
				if (repayment.closeTxnFlag == "Y") {
					repayment.holdFeeFlag = "N";
					repayment.holdIntFlag = "N";
				}
			} else {
				repayment.closeTxnFlag = "N";
				repayment.holdFeeFlag = repayment.holdFeeFlag || "N";
				repayment.holdIntFlag = repayment.holdIntFlag || "N";
			}
		}
		function repayAmtChanged(repayment) {
			if (repayment.repayType == "W") {
				if (repayment.repayAmt == "") {
					repayment.repayAmt = 0;
				}
				if (repayment.loan && repayment.loan.loanExt && repayment.repayAmt) {
					if (arithService.eq(repayment.loan.loanExt.princOutstdAmt, repayment.repayAmt)) {
						repayment.finalRepayFlag = "Y";
					} else if (arithService.gt(repayment.loan.loanExt.princOutstdAmt, repayment.repayAmt)) {
						repayment.finalRepayFlag = "N";
					}
				}
			}
		}
		function payPrincAmtChanged(repayment) {
			if (repayment.repayPrincAmt == "") {
				repayment.repayPrincAmt = 0;
			}
			if (repayment.loan && repayment.loan.loanExt && repayment.repayPrincAmt) {
				if (arithService.eq(repayment.loan.loanExt.princOutstdAmt, repayment.repayPrincAmt)) {
					repayment.finalRepayFlag = "Y";
				} else if (arithService.gt(repayment.loan.loanExt.princOutstdAmt, repayment.repayPrincAmt)) {
					repayment.finalRepayFlag = "N";
				}
			}
		}

		function repayTypeChanged(repayment, gridConfigs) {
			if (gridConfigs) {
				if (repayment.repayType == "W") {
					gridConfigs.payCcy.label = "label.repayment.writeoffccy";
					gridConfigs.payAmt.label = "label.repayment.writeoffamt";
				} else {
					gridConfigs.payCcy.label = "label.repayment.payccy";
					gridConfigs.payAmt.label = "label.repayment.payamt";
				}
			}
			if (repayment.repayType == "R") {
				repayment.rolloverTxnCcy = repayment.loan.loanInfo.loanCcy;
				repayment.rolloverProdCode = repayment.loan.loanInfo.prodCode;
			} else {
				repayment.rolloverTxnCcy = "";
				repayment.rolloverProdCode = "";
			}
			repayAmtChanged(repayment);
			payPrincAmtChanged(repayment);
		}

		return {
			initData : initData,
			getRepaymentLayout : getRepaymentLayout,
			getWriteOffLayout : getWriteOffLayout,
			getRolloverLayout : getRolloverLayout,
			getLoanViewRepaymentLayout : getLoanViewRepaymentLayout,
			finalRepayFlagChanged : finalRepayFlagChanged,
			repayAmtChanged : repayAmtChanged,
			payPrincAmtChanged : payPrincAmtChanged,
			repayTypeChanged : repayTypeChanged
		};

	}
})(angular);
